package com.example.ui.learn;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import com.example.huashangapp.R;
import com.example.net.AnalyzingJson;
import com.example.net.NetRequest;
import com.example.ui.learn.util.NumericWheelAdapter;
import com.example.ui.learn.util.WheelMain;

import com.example.ui.register.SelectProvince;
import com.example.ui.share.IndustryFragment;

import com.example.util.OnWheelChangedListener;
import com.example.util.WheelView;

import android.app.AlertDialog;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.format.Time;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import android.widget.ToggleButton;

public class StartFragment1 extends Fragment implements OnCheckedChangeListener {
	
	
	
	Boolean timeChange = false;
	//
	private LinearLayout sa_ll, l_price;//整体布局 ，隐藏的底部布局，价格的布局
	private RelativeLayout sa_rltime;//时间布局
	private TextView sa_tvtime;//时间
	private ToggleButton mToggleButton;//是否免费选择器
	private RelativeLayout sf1_rltype;
	private EditText ettheme, etstartpeople, etplace, etprice;
	private EditText ia_etcompany, ia_etaddress, ia_etname, ia_etmobilephone,
			ia_etemail, ia_etphone;
	
	private TextView sf1_tvtype;
    
	AlertDialog infoad;
	AlertDialog.Builder bulider;
    TimePicker tp;
    DatePicker dp;
    Button okfordp;
    
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View v = inflater.inflate(R.layout.startfragment1, null);
		init(v);
		//
		
		mToggleButton = (ToggleButton) v.findViewById(R.id.tglSound); // 获取到控件
		mToggleButton.setOnCheckedChangeListener(this);// 添加监听事件
        l_price=(LinearLayout) v.findViewById(R.id.l_price);
		// 时间选择器
		Calendar c = Calendar.getInstance();
		final int curHours = c.get(Calendar.HOUR_OF_DAY);
		final int curMinutes = c.get(Calendar.MINUTE);
		
		sa_ll = (LinearLayout) v.findViewById(R.id.as_ll);
		//
		sa_rltime = (RelativeLayout) v.findViewById(R.id.as_rltime);			
		sa_tvtime = (TextView) v.findViewById(R.id.as_tvtime);
		//
		ia_etcompany = (EditText) v.findViewById(R.id.ai_etcompany);
		ia_etaddress = (EditText) v.findViewById(R.id.ai_etaddress);
		ia_etname = (EditText) v.findViewById(R.id.ai_etname);
		ia_etmobilephone = (EditText) v.findViewById(R.id.ai_etmobile);
		ia_etemail = (EditText) v.findViewById(R.id.ai_etemail);
		ia_etphone = (EditText) v.findViewById(R.id.ai_etphone);
		//
		ettheme = (EditText) v.findViewById(R.id.sf1_theme);
		etstartpeople = (EditText) v.findViewById(R.id.sf1_startpeople);
		etplace = (EditText) v.findViewById(R.id.sf1_etaddress);
		etprice = (EditText) v.findViewById(R.id.sf1_price);
		sf1_rltype = (RelativeLayout) v.findViewById(R.id.sf1_type);
		sf1_tvtype = (TextView) v.findViewById(R.id.sf1_tvtype);

		if (!type.equals("")) {
			sf1_tvtype.setText(type);
		}
		sf1_rltype.setOnClickListener(sa_ocl);
		sa_ll.setOnClickListener(sa_ocl);
		sa_rltime.setOnClickListener(sa_ocl);
		return v;
	}

	// top
	private Button btnleft, btnright;
	private TextView tvtitle;
	private List<String> list;
	public static String theme = "", time = "", startpeople = "", place = "", price ="0",
			company = "", address = "", name = "", mobilephone = "",
			email = "", phone = "", type = "";
	String data, result;
	public static int typeId = -1;

	private void init(View v) {
		// top
		btnleft = (Button) v.findViewById(R.id.layout_title_left_btn);
		btnright = (Button) v.findViewById(R.id.layout_title_right_btn);
		tvtitle = (TextView) v.findViewById(R.id.layout_title_middle_text);
		btnleft.setText("返回");
		tvtitle.setText("发起活动");
		btnright.setText("提交");
		btnright.setOnClickListener(sa_ocl);
		btnleft.setOnClickListener(sa_ocl);
	}

	private void sumbit() {

		getString();

		list = new ArrayList<String>();
		list.add(theme);
		list.add(StartFragment1.typeId+"");
		list.add(startpeople);
		time = time.replace("\t", "");
		list.add(time+":00");
		list.add(place);
		list.add(price);
		list.add(company);
		list.add(address);
		list.add(name);
		list.add(mobilephone);
		list.add(email);
		list.add(phone);
		Log.i("start list", list.toString());
		Boolean isNull = null;
		for (int i = 0; i < list.size(); i++) {
			String s = list.get(i);
			if (s.equals("")) {
				Toast.makeText(getActivity(), "请完整填写资料", Toast.LENGTH_SHORT)
						.show();
				isNull=true;
				break;
			}
			isNull=false;
		}
		if(isNull==false){
		new Thread(sasumbit_run).start();
		}

		

	}

	Runnable sasumbit_run = new Runnable() {

		@Override
		public void run() {
		
			data = NetRequest.postRequestStart(list);		
			Log.i("start", list.toString());
			h.sendEmptyMessage(2);
		}
	};
	private Handler h = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {

			case 2:
				try {
					JSONObject j=new JSONObject(data);
					result=j.optString("response");
				} catch (JSONException e) {
					
					e.printStackTrace();
				}
				if (result.equals("true")) {
					Toast.makeText(getActivity(), "提交成功", Toast.LENGTH_LONG)
							.show();
					getActivity().finish();
				} else {
					Toast.makeText(getActivity(), "提交失败，请重试", Toast.LENGTH_LONG)
							.show();
				}

				break;
			default:
				break;
			}

		};

	};

	private OnClickListener sa_ocl = new OnClickListener() {

		@Override
		public void onClick(View v) {
			
			switch (v.getId()) {
			case R.id.layout_title_right_btn:
				sumbit();

				break;
			case R.id.layout_title_left_btn:
				getActivity().onBackPressed();
				break;

			case R.id.sf1_type:
				IndustryFragment tf = new IndustryFragment();
				tf.type = 2;
				FragmentManager fm = getFragmentManager();
				FragmentTransaction ft = fm.beginTransaction();
				ft.replace(R.id.as_ll, tf);
				ft.addToBackStack(null);
				ft.commit();
				break;
			case R.id.as_rltime:
			   initDatePicker();
			   infoad=bulider.show();
				break;
			
			

			default:
				break;
			}
		}
	};
	
	private void initDatePicker(){
		View v=View.inflate(getActivity(), R.layout.startselecttime, null);
		bulider = new AlertDialog.Builder(getActivity());
		bulider.setView(v).create();
		dp=(DatePicker) v.findViewById(R.id.dp);
		tp=(TimePicker) v.findViewById(R.id.tp);
		okfordp=(Button)v.findViewById(R.id.btnok);
		okfordp.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				time=dp.getYear()+"-"+dp.getMonth()+"-"+dp.getDayOfMonth()+"\t"+tp.getCurrentHour()+"时:"+tp.getCurrentMinute()+"分";
			    sa_tvtime.setText(time);
				infoad.dismiss();
				
			}
		});
		  Time t = new Time("GMT+8");    
	        t.setToNow();   	      		
		dp.init(t.year, t.month, t.monthDay, new OnDateChangedListener() {
			
			@Override
			public void onDateChanged(DatePicker view, int year, int monthOfYear,
					int dayOfMonth) {
				
				
			}
		});
		tp.setIs24HourView(true);
		
	}


	

	public void getString() {

		theme = ettheme.getText().toString();
		startpeople = etstartpeople.getText().toString();

		place = etplace.getText().toString();

		company = ia_etcompany.getText().toString();
		address = ia_etaddress.getText().toString();
		name = ia_etname.getText().toString();
		mobilephone = ia_etmobilephone.getText().toString();
		email = ia_etemail.getText().toString();
		phone = ia_etphone.getText().toString();

	}

	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

		if (isChecked) {
			price = "0";
			etprice.setText("");
			Log.i("check", "yes");
			l_price.setVisibility(View.GONE);
		} else {
			Log.i("check", "no");
			l_price.setVisibility(View.VISIBLE);
			price = etprice.getText().toString();
		}

	}

}
